package atm;


import java.util.Scanner;

/**
 * 方法版本的ATM
 */
public class Atm4 {
    public static Scanner sc = new Scanner(System.in);
    public User[] users = new User[]{
            new User("aaaaaa","111111",1000),
            new User("bbbbbb","222222",2000),
            new User("cccccc","333333",3000),
            new User("dddddd","444444",4000),
            new User("eeeeee","555555",5000)
    };
    public static int index;

    /**
     * 主流程方法
     */
    public void process() {

        System.out.println("欢迎使用XXX银行");
        login();
        menu();
    }

    /**
     * 登录
     */

    public void login() {

        for (int i = 0; i < 3; i++) {
            System.out.println("请输入账号：");
            String account = sc.next();
            System.out.println("请输入密码：");
            String pwd = sc.next();
            for (int j = 0; j < users.length; j++) {
                if (users[j].getAccount().equals(account) && users[j].getPwd().equals(pwd)) {
                    System.out.println("登陆成功");
                    index = j;
                    return;
                }
            }
            System.out.println("登录失败，还剩" + (2 - i) + "次");
            if (i == 2) {
                System.out.println("卡已被吞");
                System.exit(0);
            }
        }
    }


    /**
     * 菜单
     */

    public void menu () {
        while (true) {
            System.out.println("请选择：1-存款 2-取款 3-查询 4-转账 5-切换账号 6-修改密码 7-退出");
            int input = sc.nextInt();
            if (input == 1) {
                save();
            } else if (input == 2) {
                get();
            } else if (input == 3) {
                search();
            } else if (input == 4) {
                transfer();
            } else if (input == 5) {
                login();
            } else if (input == 6) {
                changePwd();
            } else if (input == 7){
                System.out.println("再见");
                System.exit(0);
            }
        }
    }
    /**
     * 存款
     */

    public  void save () {
        System.out.println("请输入存的金额：");
        double blance = sc.nextDouble();
        users[index].setBlance(users[index].getBlance() + blance);
        System.out.println("存款成功");

    }

    /**
     * 取款
     */
    public  void get () {
        System.out.println("请输入取款的金额：");
        double blance = sc.nextDouble();
        if (blance > (users[index].getBlance())) {
            System.out.println("余额不足");
        } else {
            users[index].setBlance (users[index].getBlance() - blance);
            System.out.println("取款成功");
        }
    }
    /**
     * 查询余额
     */
    public void search () {
        System.out.println("您的余额为：" + users[index].getBlance());
    }

    /**
     * 转账
     */
    public void transfer () {
        System.out.println("请输入对方账号：");
        String otherAccount = sc.next();
        // 不能转账给自己
        if (otherAccount.equals(users[index].getAccount())) {
            System.out.println("不能转给自己");
            return; }
        // 判断对方账号是否存在
        boolean isExist = false;
        int otherIndex = 0;
        for (int i = 0; i < users.length; i++) {
            if (users[i].getAccount().equals(otherAccount)) {
                isExist = true;
                otherIndex = i;
                break;
            }
        }
        if (!isExist) {
            System.out.println("输入的对方账号错误");
        }
        System.out.println("请输入转账金额：");
        double blance = sc.nextDouble();
        // 判断余额是否足够
        if (blance > (users[index].getBlance())) {
            System.out.println("余额不足");
            return;
        }
        // 从当前账号上扣去转账的金额
        users[index].setBlance((users[index].getBlance()) - blance );
        // 将转账的金额加在对方账号上
        users[otherIndex].setBlance(users[otherIndex].getBlance() + blance);

        System.out.println("转账成功");
    }

    /**
     * 修改密码
     */
    public void changePwd () {
        System.out.println("请输入原密码：");
        String pwd = sc.next();
        if (pwd.equals(users[index].getPwd())) {
            System.out.println("请输入新密码：");
            String newPwd1 = sc.next();
            System.out.println("请重新输入新密码：");
            String newPwd2 = sc.next();
            if (newPwd1.equals(newPwd2)){
                users[index].setPwd(newPwd1);
                System.out.println("修改成功，请重新登陆");
                process();
            }else {
                System.out.println("输入的新密码不一致，请重新选择服务类型");
            }
        } else {
            System.out.println("原密码输入错误，请重新选择服务类型");
        }

    }
    public static void main (String[]args){
        Atm4 atm4 = new Atm4();
        atm4.process();
    }
}

